<?php
namespace Org\Net;

class HttpRequests {

    public $buffer = null;  // buffer 获取返回的字符串
    public $referer = null;  // referer 设置 HTTP_REFERER 的网址
    public $response = null; // response 服务器响应的 header 信息
    public $request = null;  // request 发送到服务器的 header 信息
    private $args = null;

    public static function init(&$instanceof, $args = array()) {
        return $instanceof = new self($args);
    }

    private function __construct($args = array()) {

        if(!is_array($args)) $args = array();
        $this->args = $args;
        if(!empty($this->args['debugging'])) {
            ob_end_clean();
            set_time_limit(0);
            header('Content-Type: text/plain; charset=utf-8');
        }

    }

    public function get($url, $data = null, $cookie = null) {
        $parse = parse_url($url);
        $url .= isset($parse['query']) ? '&'. $data : ( $data ? '?'. $data : '' );
        $host = $parse['host'];

        $header  = 'Host: '. $host. "\r\n";
        $header .= 'Connection: close'. "\r\n";
        $header .= 'Accept: */*'. "\r\n";
        $header .= 'User-Agent: '. ( isset($this->args['userAgent']) ? $this->args['userAgent'] : $_SERVER['HTTP_USER_AGENT'] ). "\r\n";
        $header .= 'DNT: 1'. "\r\n";
        if($cookie) $header .= 'Cookie: '. $cookie. "\r\n";
        if($this->referer) $header .= 'Referer: '. $this->referer. "\r\n";

        $options = array();
        $options['http']['method'] = 'GET';
        $options['http']['header'] = $header;

        $response = get_headers($url);
        $this->request = $header;
        $this->response = implode("\r\n", $response);
        $context = stream_context_create($options);
        return $this->buffer = file_get_contents($url, false, $context);

    }

    public function post($url, $data = null, $cookie = null) {

        $parse = parse_url($url);
        $host = $parse['host'];

        $header  = 'Host: '. $host. "\r\n";
        $header .= 'Connection: keep-alive'. "\r\n";
        $header .= 'X-Access-Code: lovepinyaoaccesscode'."\r\n";
        $header .= 'X-Access-Key: lovepinyaoaccesskey'."\r\n";
        $header .= 'Accept: */*'. "\r\n";
        $header .= 'User-Agent: '. ( isset($this->args['userAgent']) ? $this->args['userAgent'] : $_SERVER['HTTP_USER_AGENT'] ). "\r\n";
        $header .= 'Content-Type: application/x-www-form-urlencoded'. "\r\n";
        $header .= 'DNT: 1'. "\r\n";
        if($cookie) $header .= 'Cookie: '. $cookie. "\r\n";
        if($this->referer) $header .= 'Referer: '. $this->referer. "\r\n";
        if($data) $header .= 'Content-Length: '. strlen(http_build_query($data)). "\r\n";

        $options = array();
        $options['http']['method'] = 'POST';
        $options['http']['header'] = $header;
        if($data) $options['http']['content'] = http_build_query($data);

        $response = get_headers($url);
        $this->request = $header;
        $this->response = implode("\r\n", $response);
        $context = stream_context_create($options);
        return $this->buffer = file_get_contents($url, false, $context);

    }

    function curl_request($url,$post='',$cookie='', $returnCookie=0){
        $curl = curl_init();
        $this_header = array(
            "content-type: application/x-www-form-urlencoded;charset=UTF-8",
            "X-Access-Code: lovepinyaoaccesscode",
            "X-Access-Key: lovepinyaoaccesskey"
        );

        curl_setopt($curl,CURLOPT_HTTPHEADER,$this_header);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
        curl_setopt($curl, CURLOPT_REFERER, "http://www.lovepinyao.com");
        if($post) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
        }
        if($cookie) {
            curl_setopt($curl, CURLOPT_COOKIE, $cookie);
        }
        curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        if (curl_errno($curl)) {
            return curl_error($curl);
        }
        curl_close($curl);
        if($returnCookie){
            list($header, $body) = explode("\r\n\r\n", $data, 2);
            preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
            $info['cookie']  = substr($matches[1][0], 1);
            $info['content'] = $body;
            return $info;
        }else{
            return $data;
        }
    }

}